//
//  DWJVisitorView.swift
//  weibo
//
//  Created by 代伟佳 on 16/4/17.
//  Copyright © 2016年 代伟佳. All rights reserved.
//

import UIKit
import SnapKit

class DWJVisitorView: UIView {
    
    var visistorClosure: (() -> ())?
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 外界调用方法
    func setVisitorInfo(imageName: String?, title: String?) {
        
        if let img = imageName, tit = title {
            
            // 消息 发现 我
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            circleImageView.hidden = true

            
        } else {
        
            // 主页
            setCircleAnim()

        }
    }
    
    
    // MARK: - 监听事件
    @objc private func loginClick() {
    
        visistorClosure?()
    }
    
    // 让圆圈转起来
    private func setCircleAnim() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.duration = 20
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        circleImageView.layer.addAnimation(anim, forKey: nil)
    }

    private func setUpUI() {
    
        backgroundColor = RGB(red: 237, green: 237, blue: 237)
//        backgroundColor = RandomColor()
        
        // 添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 添加约束 
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.height.equalTo(36)
            make.width.equalTo(224)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
    }
    
    // MARK: - 懒加载控件
    private lazy var circleImageView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return imageView
    }()
    
    private lazy var maskImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
       
        return imageView
    }()
    
    private lazy var iconImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return imageView
    }()
    
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.grayColor(), textFont: 14)
        label.text = "关注一些人，回这里看看有什么惊喜";
        label.textAlignment = .Center
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var registerButton: UIButton = {
    
        let button = UIButton()
        
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14) 
        return button
    }()
    
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        return button
        }()
}
